<?php


namespace app\web\controller;


use app\web\model\Comments;
use think\Db;
use think\facade\Request;
use think\facade\Session;

class Console
{
    public function count1()
    {
        $lastTime = Session::get('lastSearchTime');
        if ($lastTime == null) {
            $timeMsg = "无";
        } else {
            $nowTime = time();
            $Interval = $nowTime - $lastTime;
            if ($Interval < 60)
                $timeMsg = $Interval . '秒前';
            elseif ($Interval < 3600)
                $timeMsg = floor($Interval / 60) . '分钟前';
            elseif ($Interval < 86400)
                $timeMsg = floor($Interval / 3600) . '小时前';
            elseif ($Interval < 1728000)
                $timeMsg = floor($Interval / 86400) . '天前';
            else
                $timeMsg = date("Y-m-d", $lastTime);
        }
        return $timeMsg;
    }

    //爬取总数
    public function count2()
    {
        return Comments::where('Source', 0)->count();
    }

    //预测总数
    public function count3()
    {
        $where = array();
        return Comments::where('Theme', 'not null')->count();
    }

    //负评总数
    public function count4()
    {
        return Comments::where('Emotional', '不满意')->count();
    }

    public function prid()
    {
        $a = Comments::count();
        $b = Comments::where('Emotional', null)->count();
        return [$a, $b];
    }

    public function map()
    {
        return Db::query("
            select n.year as year,p.Num as '正面评论',n.num as '负面评论' from(
                select year(ReleaseDate)as year,Count(Pid) as num
                from comments
                WHERE `Emotional`='满意'
                group by year(ReleaseDate)
            )as p,(
                select year(ReleaseDate)as year,Count(Pid) as num
                from comments
                WHERE `Emotional`='不满意'
                group by year(ReleaseDate)
            )as n
            where p.year=n.year");
    }

    public function carCount()
    {
        $carModel = Request::param('carModel');
        return Db::table('comments')
            ->where('carModel', 'like', '%' . $carModel . '%')
            ->count();
    }
}